﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Micrhard.FMCG.Server.Interface;
using Microhard.Core.DBUtility;
using Micrhard.FMCG.Server.DataAccess;
using Microhard.Core;
using Micrhard.FMCG.Server.Msg;
using Micrhard.FMCG.Server.Models;
using Micrhard.FMCG.Server.DTO;

namespace Micrhard.FMCG.Server.Impl
{
    public class CustomerCoinImpl : ICoinsMng
    {
        public Microhard.Core.DBUtility.ExecutedResult Add(Models.CustomerCoinModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.CustomerCoinModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.CustomerCoins.Where(l => ids.Contains(l.CoinID));
            foreach (var e in entity)
            {
                e.Coins = 0;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, CustomerCoinMsg.DeleteFailed);
                Log.Error("ICoinsMng.Delete", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.CustomerCoinModel model)
        {
            throw new NotImplementedException();
        }

        public Models.CustomerCoinModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.CustomerCoinModel GetModelByModel(Models.CustomerCoinModel model)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.CustomerCoins.SingleOrDefault(c => c.CustomerID == model.CustomerID);
            if (entity != null)
                return entity.ToModel();
            return null;
        }

        public List<Models.CustomerCoinModel> Query(Models.CustomerCoinModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            IEnumerable<CustomerCoin> entities = from b in db.CustomerCoins
                                                 where b.Customer.CompanyID == this.scope.EmpInfo.CompanyID
                                                 select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.Coins).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<CustomerCoinModel>();
        }

        public List<Models.CustomerCoinModel> Query(Models.CustomerCoinModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public CustomerCoinModel GetCustomerCoinByOpenID(string openID, Guid companyID)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.CustomerCoins.SingleOrDefault(c => c.Customer.OpenID == openID && c.Customer.CompanyID == companyID);
            if (entity != null)
            {
                return  entity.ToModel();
            }
            return new CustomerCoinModel();
        }
    }
}
